package com.gsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gsc.constants.SystemConstants;
import com.gsc.domain.ResponseResult;
import com.gsc.domain.dto.RoleDto;
import com.gsc.domain.vo.AddRoleVo;
import com.gsc.domain.dto.RoleListDto;
import com.gsc.domain.entity.Role;
import com.gsc.domain.entity.RoleMenu;
import com.gsc.domain.vo.PageVo;
import com.gsc.mapper.RoleMapper;
import com.gsc.service.RoleMenuService;
import com.gsc.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gsc.service.RoleService;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2023-02-16 13:59:49
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员  如果是返回集合中 只需要有admin
        if(id == 1L){
            ArrayList<String> roleKey = new ArrayList<>();
            roleKey.add("admin");
            return roleKey;
        }
        //如果不是管理员  查询当前用户所具有的的角色信息

        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public PageVo selectRoleList(Integer pageNum, Integer pageSize, RoleListDto roleListDto) {

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(roleListDto.getRoleName()),Role::getRoleName,roleListDto.getRoleName());
        queryWrapper.eq(StringUtils.hasText(roleListDto.getStatus()),Role::getStatus,roleListDto.getStatus());
        queryWrapper.orderByAsc(Role::getRoleSort);
        //分页查询
        Page<Role> rolePage = new Page<>();
        page(rolePage,queryWrapper);
        //封装成pageVo返回
        PageVo pageVo = new PageVo(rolePage.getRecords(),rolePage.getTotal());
        return pageVo;
    }

    @Override
    public ResponseResult addRole(AddRoleVo addRoleVo) {

        Role role = BeanCopyUtils.copyBean(addRoleVo, Role.class);
        //新增角色
        roleMapper.insert(role);

        //将menuId转换成RoleMenu
        List<RoleMenu> roleMenus = addRoleVo.getMenuIds().stream()
                .map(menuId -> new RoleMenu(role.getId(), menuId.longValue()))
                .collect(Collectors.toList());

        //新增角色和菜单关联表
        roleMenuService.saveBatch(roleMenus);
        return ResponseResult.okResult();
    }


    @Override
    public ResponseResult updateRole(RoleDto roleDto) {
        Role role = BeanCopyUtils.copyBean(roleDto, Role.class);
        //根据角色id更新角色
        getBaseMapper().updateById(role);
        //更新角色菜单中间表，先删除原来的，在插入新的
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,roleDto.getId());
        roleMenuService.remove(queryWrapper);

        List<RoleMenu> roleMenus = Arrays.stream(roleDto.getMenuIds())
                .map(menuId -> new RoleMenu(roleDto.getId(), menuId))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenus);
        return ResponseResult.okResult();
    }

    /**
     * 查询所有状态正常的角色
     * @return
     */
    @Override
    public List<Role> listAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.STATUS_NORMAL);
        return list(queryWrapper);
    }



}
